package com.ghw.neteasemusic.modules.song.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ghw.neteasemusic.common.processor.FileToDataBaseProcessor;
import com.ghw.neteasemusic.common.result.R;
import com.ghw.neteasemusic.modules.song.domain.Song;
import com.ghw.neteasemusic.modules.song.service.SongService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * 歌曲(Song)表控制层
 *
 * @author violet
 * @since 2021-12-09 21:59:18
 */
@RestController
@AllArgsConstructor
@RequestMapping("song")
public class SongController {
    /**
     * 服务对象
     */
    private final SongService songService;
    private final FileToDataBaseProcessor processor;

    @GetMapping("import")
    public R<Void> importAll() {
        processor.importSongs();
        return R.success();
    }

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param song 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R<Page<Song>> selectAll(Page<Song> page, Song song) {
        return R.success(this.songService.page(page, new QueryWrapper<>(song)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R<Song> selectOne(@PathVariable Serializable id) {
        return R.success(this.songService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param song 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R<Boolean> insert(@RequestBody Song song) {
        return R.success(this.songService.save(song));
    }

    /**
     * 修改数据
     *
     * @param song 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R<Boolean> update(@RequestBody Song song) {
        return R.success(this.songService.updateById(song));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R<Boolean> delete(@RequestParam("idList") List<Long> idList) {
        return R.success(this.songService.removeByIds(idList));
    }
}

